package com.tsu.mall.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.tsu.mall.common.config.AlipayConfigBean;
import com.tsu.mall.common.entity.Bill;
import com.tsu.mall.common.entity.BillPay;
import com.tsu.mall.common.mapper.BillDetailMapper;
import com.tsu.mall.common.service.IBillPayService;
import com.tsu.mall.common.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Service
public class PayServiceImpl implements IPayService {
@Autowired
private AlipayConfigBean alipayConfigBean;
@Autowired
private IBillPayService iBillPayService;
@Autowired
private BillDetailMapper billDetailMapper;
    //对接支付
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public String createPayBill(Bill bill) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",alipayConfigBean.getAppid(),alipayConfigBean.getPrivateKey(),"json","UTF-8",alipayConfigBean.getPublicKey(),alipayConfigBean.getSignType());
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("  http://cmmnb8.natappfree.cc/bill/notify");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", bill.getCode());
        bizContent.put("total_amount",bill.getPrice());
        bizContent.put("subject", "LTMALL-测试商品");

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            return response.getQrCode();//返回二维码地址
        } else {
            System.out.println("调用失败");
        }
        return null;
    }

    //用于记录支付宝回调信息
    @Override
    public void payResult(Map<String, String> params) {
        DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        BillPay billPay=new BillPay();
        billPay.setBillId(billDetailMapper.doSelectByCode(params.get("out_trade_no")));
        billPay.setTradeNo(params.get("trade_no"));
        billPay.setOutTradeNo(params.get("out_trade_no"));
        billPay.setBuyerId(params.get("buyer_id"));
        billPay.setBuyerLogonId(params.get("buyer_logon_id"));
        billPay.setBuyerPayAmount(new BigDecimal(params.get("buyer_pay_amount")));
        billPay.setGmtCreate(LocalDateTime.parse(params.get("gmt_create"),df));
        billPay.setGmtPayment(LocalDateTime.parse(params.get("gmt_payment"),df));
        billPay.setNotifyTime(LocalDateTime.parse(params.get("notify_time"),df));
        iBillPayService.save(billPay);
    }
}
